package com.test.first;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyList3Activity extends AppCompatActivity implements AdapterView.OnItemClickListener , AdapterView.OnItemLongClickListener {


    private static final String TAG = "MyList3Activity";
    ListView listView;
    MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list3);
        listView = findViewById(R.id.mylist3);
        ProgressBar progressBar = findViewById(R.id.progressBar);

/*
        listView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        //准备数据
        ArrayList<HashMap<String,String>>listItems = new ArrayList<HashMap<String, String>>();
        for (int i=0;i<10;i++ ){
            HashMap<String,String>map  = new HashMap<String, String>();
            map.put("ItemTitle","Rate:"+ i);//标题文字
            map.put("ItemDetail","detail :"+ i);//详情描述
            listItems.add(map);
        }

        //适配器
        SimpleAdapter adapter = new SimpleAdapter(this,
                listItems,//listItems数据源
                R.layout.list_item,//ListItem的XML布局实现
                new  String[]{"ItemTitle", "ItemDetail"},
                new int[]{R.id.itemTitle,R.id.itemDetail}
                );
        listView.setAdapter(adapter);
*/


        Handler handler = new Handler(){
            @Override
            public void handleMessage(@Nullable Message msg){
                if(msg.what==9){
                    ArrayList<HashMap<String,String>>retList = (ArrayList<HashMap<String, String>>)msg.obj;

//                    SimpleAdapter adapter = new SimpleAdapter(MyList3Activity.this,
//                            retList,//listItems数据源
//                            R.layout.list_item,//ListItem的XML布局实现
//                            new  String[]{"ItemTitle", "ItemDetail"},
//                            new int[]{R.id.itemTitle,R.id.itemDetail}
//                    );

                    adapter = new MyAdapter(MyList3Activity.this,R.layout.list_item,retList);

                    listView.setAdapter(adapter);
                    //切换显示
                    listView.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);

                }
                super.handleMessage(msg);
            }
        };

        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);


        MapTask task = new MapTask();
        task.setHandler(handler);

        Thread t = new Thread(task);
        t.start();//task.run()

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object itemAtPosition = listView.getItemAtPosition(position);
        HashMap<String,String>map = (HashMap<String,String>)itemAtPosition;
        String titleStr = map.get("ItemTitle");
        String detailStr = map.get("ItemDetail");
        Log.i(TAG,"onItemClick: titleStr="+ titleStr);
        Log.i(TAG,"OnItemClick: detailStr=" + detailStr);



        //打开窗口
        Intent calc = new Intent(this,CalcActivity.class);
        calc.putExtra("title",titleStr);
        calc.putExtra("rate", detailStr);
        startActivity(calc);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"onItemLongClick:长按事件处理 position= " +position);
        //删除数据
        adapter.remove( listView.getItemAtPosition(position));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
        .setMessage("请确认是否删除当前数据")
        .setPositiveButton("是",new DialogInterface.OnClickListener(){


            @Override
                    public void onClick(DialogInterface dialog,int which){
                Log.i(TAG,"onclick: 对话框处理");
                //删除数据项
                adapter.remove( listView.getItemAtPosition(position));
            }
        }).setNegativeButton("否",null);
        builder.create().show();

        return true;
    }
}